package com.poetize.plus.config;

import com.poetize.plus.enums.QiNiuAddressEnum;
import com.poetize.plus.properties.QiNiuProperties;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author zuosy
 * @created 2024/4/1 9:07
 */
@Configuration
@RequiredArgsConstructor
public class QiNiuClient {

    private final QiNiuProperties qiNiuProperties;

    /**
     * 华东机房,配置自己空间所在的区域
     */
    @Bean
    public com.qiniu.storage.Configuration qiNiuConfig() {
        if (qiNiuProperties.getAddress().equals(QiNiuAddressEnum.HUA_DONG_ZJ)){
            return new com.qiniu.storage.Configuration(Region.huadong());
        }
        if (qiNiuProperties.getAddress().equals(QiNiuAddressEnum.HUA_BEI)){
            return new com.qiniu.storage.Configuration(Region.huabei());
        }
        if (qiNiuProperties.getAddress().equals(QiNiuAddressEnum.HUA_NAN)){
            return new com.qiniu.storage.Configuration(Region.huanan());
        }
        if (qiNiuProperties.getAddress().equals(QiNiuAddressEnum.BEI_MEI)){
            return new com.qiniu.storage.Configuration(Region.beimei());
        }
        if (qiNiuProperties.getAddress().equals(QiNiuAddressEnum.XIN_JIA_PO)){
            return new com.qiniu.storage.Configuration(Region.xinjiapo());
        }
        if (qiNiuProperties.getAddress().equals(QiNiuAddressEnum.HUA_DONG_ZJ2)){
            return new com.qiniu.storage.Configuration(Region.huadongZheJiang2());
        }
        throw new RuntimeException("七牛云初始化指定区域异常");
    }

    /**
     * 构建一个七牛上传工具实例
     */
    @Bean
    public UploadManager uploadManager() {
        return new UploadManager(qiNiuConfig());
    }

    /**
     * 认证信息实例 * @return
     */
    @Bean
    public Auth auth() {
        return Auth.create(qiNiuProperties.getAccessKey(), qiNiuProperties.getSecretKey());
    }

    /**
     * 构建七牛空间管理实例
     */
    @Bean
    public BucketManager bucketManager() {
        return new BucketManager(auth(), qiNiuConfig());
    }
}
